Completed
Push — master ( a3e598...0882e3 )
by Maxence
01:55
created

searchbar.init   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 100
rs 8.2857

7 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 11 2
A 0 11 1
A 0 4 1
A 0 7 2
A 0 3 1
A 0 8 4
A 0 17 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
 * FullTextSearch - Full text search framework for Nextcloud
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OCA */
27
/** global: nav */
28
/** global: _ */
29
/** global: api */
30
/** global: search */
31
/** global: result */
32
/** global: fullTextSearch */
33
/** global: settings */
34
35
36
var searchbox = {
37
	searchTimeout: null,
38
	search_more: null,
39
	search_icon_more: null,
40
	search_icon_close: null,
41
	search_icon: null,
42
	search_input: null,
43
	search_form: null
44
};
45
46
47
var searchbar = {
48
49
	init: function () {
50
		var divHeaderRight = $('div.header-right');
51
52
		searchbox.search_div = $('<div>', {class: 'next_search_div'});
53
		divHeaderRight.prepend(searchbox.search_div);
54
55
		searchbox.search_icon = $('<div>', {class: 'icon-fulltextsearch'});
56
		searchbox.search_icon.css('background-image',
57
			"url('/apps/fulltextsearch/img/fulltextsearch.svg')");
58
		searchbox.search_icon.fadeTo(0, 0.7);
59
		searchbox.search_div.append(searchbox.search_icon);
60
61
		searchbox.search_form = $('<div>');
62
		searchbox.search_form.fadeTo(0, 0);
63
64
		searchbox.search_input = $('<input>', {
65
			id: 'next_search_input',
66
			placeholder: 'Search'
67
		});
68
		searchbox.search_form.append(searchbox.search_input);
69
70
		searchbox.search_more = $('<div>', {class: 'search_more'});
71
		searchbox.search_more.fadeTo(0, 0).hide();
72
73
		searchbox.search_icon_more = $('<div>', {class: 'icon-more-white icon-more-fulltextsearch'});
74
		searchbox.search_icon_more.fadeTo(0, 0);
75
		searchbox.search_icon_more.on('click', function () {
76
			if (curr.moreDisplayed) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
77
				searchbox.search_more.stop().fadeTo(100, 0, function () {
78
					$(this).hide();
79
				});
80
				curr.moreDisplayed = false;
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
81
			} else {
82
				searchbox.search_more.stop().show().fadeTo(100, 1);
83
				curr.moreDisplayed = true;
84
			}
85
		});
86
		searchbox.search_form.append(searchbox.search_icon_more);
87
88
		searchbox.search_icon_close = $('<div>', {class: 'icon-close-white icon-close-fulltextsearch'});
89
		searchbox.search_icon_close.fadeTo(0, 0);
90
		searchbox.search_icon_close.on('click', function () {
91
			settings.lockSearchbox = false;
92
			searchbox.search_icon_more.stop().fadeTo(100, 0);
93
			searchbox.search_icon_close.stop().fadeTo(100, 0);
94
			searchbox.search_more.stop().fadeTo(100, 0, function () {
95
				$(this).hide();
96
			});
97
			curr.moreDisplayed = false;
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
98
			searchbox.search_input.val('');
99
			nav.onSearchReset();
100
		});
101
		searchbox.search_form.append(searchbox.search_icon_close);
102
103
		searchbox.search_form.hover(function () {
104
			searchbox.search_icon.stop().fadeTo(100, 0);
105
			searchbox.search_form.stop().fadeTo(100, 0.8);
106
		}, function () {
107
			if (settings.lockSearchbox === true) {
108
				return;
109
			}
110
			searchbox.search_form.stop().fadeTo(500, 0);
111
			searchbox.search_icon.stop().fadeTo(800, 0.7);
112
		});
113
		searchbox.search_div.append(searchbox.search_form);
114
		searchbox.search_div.append(searchbox.search_more);
115
116
		searchbox.search_input.on('focus', function () {
117
			searchbar.searching();
118
		});
119
120
		searchbox.search_input.on('input', function () {
121
122
			if ($(this).val() === '') {
123
				nav.onSearchReset();
124
			}
125
126
			// if (settings.parentHasMethod('onEntryGenerated')) {
127
			// 	settings.parent.onEntryGenerated();
128
			// }
129
130
			if (searchbox.searchTimeout === null && searchbar.initSearch(false)) {
131
				searchbox.searchTimeout = _.delay(function () {
132
					searchbar.initSearch(false);
133
					searchbox.searchTimeout = null;
134
				}, 2000);
135
			}
136
		});
137
138
		fullTextSearch.options(settings.searchProviderId);
139
140
		$(window).bind('keydown', function (event) {
141
			if (event.ctrlKey || event.metaKey) {
142
				if (String.fromCharCode(event.which).toLowerCase() === 'f') {
143
					event.preventDefault();
144
					searchbar.searching();
145
				}
146
			}
147
		});
148
	},
149
150
151
	searching: function () {
152
		if (settings.lockSearchbox === true) {
153
			return;
154
		}
155
		settings.lockSearchbox = true;
156
		searchbox.search_icon.stop().fadeTo(100, 0);
157
		searchbox.search_form.stop().fadeTo(100, 0.8);
158
		searchbox.search_input.focus();
159
		searchbox.search_icon_close.stop().fadeTo(200, 1);
160
		if (settings.noMoreOptions) {
161
			searchbox.search_icon_more.stop().fadeTo(200, 1);
162
		}
163
	},
164
165
166
	onOptionsLoaded: function (result) {
167
		if (!result[settings.searchProviderId]) {
168
			searchbox.search_icon_more.off('click').hide();
169
			searchbox.search_input.css('width', '245px');
170
			return;
171
		}
172
		settings.noMoreOptions = true;
173
		searchbox.search_more.html(result[settings.searchProviderId]);
174
		searchbox.search_more.find('INPUT').each(function () {
175
			$(this).on('change', function () {
176
				var search = searchbox.search_input.val();
177
				fullTextSearch.search({
178
					providers: settings.searchProviderId,
179
					search: search,
180
					page: curr.page,
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
181
					options: searchbar.getSearchOptions(),
182
					size: 20
183
				});
184
			});
185
		})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
186
	},
187
188
189
	getSearchOptions: function () {
190
		var options = {};
191
		searchbox.search_more.find('INPUT').each(function () {
192
			var value = $(this).val();
193
194
			if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
195
				value = '';
196
			}
197
198
			options[$(this).attr('id')] = value;
199
		});
200
201
		return options;
202
	},
203
204
205
	// TODO: do we really need this initSearch, or should we use the one from fulltextsearch.js !?
206
	initSearch: function (force) {
207
		var search = searchbox.search_input.val();
208
209
		if (!force && search.length < 3) {
210
			return false;
211
		}
212
213
		if (curr.lastRequest === search) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
214
			return true;
215
		}
216
217
		curr.lastRequest = search;
218
219
		fullTextSearch.search({
220
			providers: settings.searchProviderId,
221
			search: search,
222
			page: curr.page,
223
			options: searchbar.getSearchOptions(),
224
			size: 20
225
		});
226
227
		return true;
228
	}
229
230
231
};
232
233
234